package me.firstapp.service.zerone.cqssc.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import me.firstapp.common.utils.Page;
import me.firstapp.common.utils.StrUtils;
import me.firstapp.module.zerone.cqssc.CqsscLotteryResult;
import me.firstapp.module.zerone.cqssc.CqsscPlan;
import me.firstapp.repository.zerone.cqssc.CqsscLotteryResultRepository;
import me.firstapp.repository.zerone.cqssc.CqsscPlanRepository;
import me.firstapp.service.zerone.cqssc.CqsscPlanService;

@Service
public class CqsscPlanServiceImpl implements CqsscPlanService {

	@Autowired
	private CqsscLotteryResultRepository lotteryResultRepository;

	@Autowired
	private CqsscPlanRepository cqsscPlanRepository;

	@Override
	public void addPlans(String planIssueNo, int notWinNum) {

		String planIssueDate = planIssueNo.split("-")[0];
		int planIssueSubNum = Integer.valueOf(planIssueNo.split("-")[1]);
		// 前8期和后10期不计划
		if (planIssueSubNum >= 8 && planIssueSubNum <= 110) {
			// 判断前一期是否中奖，若没中奖则进行倍投，若中奖则进行计划
			int prePlanIssueSubNum = planIssueSubNum - 1;
			String prePlanIssueNo = planIssueDate + "-";
			if (prePlanIssueSubNum < 10) {
				prePlanIssueNo = prePlanIssueNo + "00" + prePlanIssueSubNum;
			} else if (prePlanIssueSubNum > 9 && prePlanIssueSubNum < 100) {
				prePlanIssueNo = prePlanIssueNo + "0" + prePlanIssueSubNum;
			} else if (prePlanIssueSubNum > 99) {
				prePlanIssueNo = prePlanIssueNo + prePlanIssueSubNum;
			}
			List<CqsscPlan> plans = cqsscPlanRepository.findByPlanIssueNo(prePlanIssueNo);
			int i = 0;
			for (CqsscPlan cqsscPlan : plans) {
				if (cqsscPlan.getStatus() == CqsscPlan.CQSSC_PLAN_STATUS_NOT_WIN) {
					i++;
				}
			}
			if (i < 7) {// 如果没有失败的计划则进行计划，否则按倍投处理。未中的数量只要小于7，就表示没有失败的计划。
				// 第一步，查询期号
				List<CqsscLotteryResult> lotteryResults = lotteryResultRepository
						.findListByIssueDateAndSubNum(planIssueDate, planIssueSubNum - notWinNum, planIssueSubNum);

				// 第二步，生成满足条件的计划号
				Set<String> planNos1 = new HashSet<String>();
				Set<String> planNos2 = new HashSet<String>();
				Set<String> planNos3 = new HashSet<String>();
				Set<String> planNos4 = new HashSet<String>();
				Set<String> planNos5 = new HashSet<String>();
				int maxCounts = 0;
				do {
					maxCounts++;
					String planNo = StrUtils.getNum(6);

					// 万位
					boolean flag1 = true;
					for (CqsscLotteryResult lotteryResult : lotteryResults) {
						char no1 = lotteryResult.getPrizeNo().charAt(0);
						if (planNo.contains(no1 + "")) {
							flag1 = false;
							break;
						}
					}
					if (flag1 == true) {
						planNos1.add(planNo);
					}

					// 千位
					boolean flag2 = true;
					for (CqsscLotteryResult lotteryResult : lotteryResults) {
						char no2 = lotteryResult.getPrizeNo().charAt(1);
						if (planNo.contains(no2 + "")) {
							flag2 = false;
							break;
						}
					}
					if (flag2 == true) {
						planNos2.add(planNo);
					}

					// 百位
					boolean flag3 = true;
					for (CqsscLotteryResult lotteryResult : lotteryResults) {
						char no3 = lotteryResult.getPrizeNo().charAt(2);
						if (planNo.contains(no3 + "")) {
							flag3 = false;
							break;
						}
					}
					if (flag3 == true) {
						planNos3.add(planNo);
					}

					// 十位
					boolean flag4 = true;
					for (CqsscLotteryResult lotteryResult : lotteryResults) {
						char no4 = lotteryResult.getPrizeNo().charAt(3);
						if (planNo.contains(no4 + "")) {
							flag4 = false;
							break;
						}
					}
					if (flag4 == true) {
						planNos4.add(planNo);
					}

					// 个位
					boolean flag5 = true;
					for (CqsscLotteryResult lotteryResult : lotteryResults) {
						char no5 = lotteryResult.getPrizeNo().charAt(4);
						if (planNo.contains(no5 + "")) {
							flag5 = false;
							break;
						}
					}
					if (flag5 == true) {
						planNos5.add(planNo);
					}

				} while (maxCounts < 100000);

				// 第三步，存储满足条件的计划号
				if (planNos1.size() == 7) {
					for (String planNo : planNos1) {
						CqsscPlan plan = new CqsscPlan();
						plan.setPlanIssueNo(planIssueNo);
						plan.setPlanIssueDate(planIssueDate);
						plan.setPlanIssueSubNum(planIssueSubNum);
						plan.setNotWinNum(notWinNum);
						plan.setPlanNo(planNo);
						plan.setWei("万位");
						plan.setStatus(CqsscPlan.CQSSC_PLAN_STATUS_NOT_OPEN);
						plan.setTimesNo(0);
						cqsscPlanRepository.save(plan);
					}
				}

				if (planNos2.size() == 7) {
					for (String planNo : planNos2) {
						CqsscPlan plan = new CqsscPlan();
						plan.setPlanIssueNo(planIssueNo);
						plan.setPlanIssueDate(planIssueDate);
						plan.setPlanIssueSubNum(planIssueSubNum);
						plan.setNotWinNum(notWinNum);
						plan.setPlanNo(planNo);
						plan.setWei("千位");
						plan.setStatus(CqsscPlan.CQSSC_PLAN_STATUS_NOT_OPEN);
						plan.setTimesNo(0);
						cqsscPlanRepository.save(plan);
					}
				}

				if (planNos3.size() == 7) {
					for (String planNo : planNos3) {
						CqsscPlan plan = new CqsscPlan();
						plan.setPlanIssueNo(planIssueNo);
						plan.setPlanIssueDate(planIssueDate);
						plan.setPlanIssueSubNum(planIssueSubNum);
						plan.setNotWinNum(notWinNum);
						plan.setPlanNo(planNo);
						plan.setWei("百位");
						plan.setStatus(CqsscPlan.CQSSC_PLAN_STATUS_NOT_OPEN);
						plan.setTimesNo(0);
						cqsscPlanRepository.save(plan);
					}
				}

				if (planNos4.size() == 7) {
					for (String planNo : planNos4) {
						CqsscPlan plan = new CqsscPlan();
						plan.setPlanIssueNo(planIssueNo);
						plan.setPlanIssueDate(planIssueDate);
						plan.setPlanIssueSubNum(planIssueSubNum);
						plan.setNotWinNum(notWinNum);
						plan.setPlanNo(planNo);
						plan.setWei("十位");
						plan.setStatus(CqsscPlan.CQSSC_PLAN_STATUS_NOT_OPEN);
						plan.setTimesNo(0);
						cqsscPlanRepository.save(plan);
					}
				}

				if (planNos5.size() == 7) {
					for (String planNo : planNos5) {
						CqsscPlan plan = new CqsscPlan();
						plan.setPlanIssueNo(planIssueNo);
						plan.setPlanIssueDate(planIssueDate);
						plan.setPlanIssueSubNum(planIssueSubNum);
						plan.setNotWinNum(notWinNum);
						plan.setPlanNo(planNo);
						plan.setWei("个位");
						plan.setStatus(CqsscPlan.CQSSC_PLAN_STATUS_NOT_OPEN);
						plan.setTimesNo(0);
						cqsscPlanRepository.save(plan);
					}
				}

				// 第四步，如果没有满足条件的计划号，则生成无计划的记录
				if (planNos1.size() != 7 && planNos2.size() != 7 && planNos3.size() != 7 && planNos4.size() != 7
						&& planNos5.size() != 7) {
					CqsscPlan plan = new CqsscPlan();
					plan.setPlanIssueNo(planIssueNo);
					plan.setPlanIssueDate(planIssueDate);
					plan.setPlanIssueSubNum(planIssueSubNum);
					plan.setNotWinNum(notWinNum);
					plan.setPlanNo("不计划");
					plan.setWei("无");
					plan.setStatus(CqsscPlan.CQSSC_PLAN_STATUS_NOT_PLAN);
					plan.setTimesNo(0);
					cqsscPlanRepository.save(plan);
				}
			}

		} else {
			CqsscPlan plan = new CqsscPlan();
			plan.setPlanIssueNo(planIssueNo);
			plan.setPlanIssueDate(planIssueDate);
			plan.setPlanIssueSubNum(planIssueSubNum);
			plan.setNotWinNum(notWinNum);
			plan.setPlanNo("不计划");
			plan.setWei("无");
			plan.setStatus(CqsscPlan.CQSSC_PLAN_STATUS_NOT_PLAN);
			plan.setTimesNo(0);
			cqsscPlanRepository.save(plan);
		}

	}

	@Override
	public Page<CqsscPlan> findPage(Integer pageNo, Integer pageSize, Map<String, Object> searchParams,
			String sortParam, String sortType) {
		return cqsscPlanRepository.findPage(pageNo, pageSize, searchParams, sortParam, sortType);
	}

	@Override
	public void executeSetPrizeNo(String planIssueNo, String openNo, boolean isTimes) {
		List<CqsscPlan> plans = cqsscPlanRepository.findByPlanIssueNo(planIssueNo);
		for (CqsscPlan plan : plans) {
			// 如果不是倍投，则设置开奖号码
			if (isTimes == false) {
				plan.setOpenNo(openNo);
			}

			if ("万位".equals(plan.getWei())) {
				char n = openNo.charAt(0);
				if (plan.getPlanNo().contains(n + "")) {
					plan.setStatus(CqsscPlan.CQSSC_PLAN_STATUS_WIN);
					plan.setTimesPrizeIssueNo(planIssueNo);
					plan.setPrizeNo(openNo);
				} else {
					plan.setStatus(CqsscPlan.CQSSC_PLAN_STATUS_NOT_WIN);
					plan.setTimesNo(plan.getTimesNo() + 1);
				}
			}
			if ("千位".equals(plan.getWei())) {
				char n = openNo.charAt(1);
				if (plan.getPlanNo().contains(n + "")) {
					plan.setStatus(CqsscPlan.CQSSC_PLAN_STATUS_WIN);
					plan.setTimesPrizeIssueNo(planIssueNo);
					plan.setPrizeNo(openNo);
				} else {
					plan.setStatus(CqsscPlan.CQSSC_PLAN_STATUS_NOT_WIN);
					plan.setTimesNo(plan.getTimesNo() + 1);
				}
			}
			if ("百位".equals(plan.getWei())) {
				char n = openNo.charAt(2);
				if (plan.getPlanNo().contains(n + "")) {
					plan.setStatus(CqsscPlan.CQSSC_PLAN_STATUS_WIN);
					plan.setTimesPrizeIssueNo(planIssueNo);
					plan.setPrizeNo(openNo);
				} else {
					plan.setStatus(CqsscPlan.CQSSC_PLAN_STATUS_NOT_WIN);
					plan.setTimesNo(plan.getTimesNo() + 1);
				}
			}
			if ("十位".equals(plan.getWei())) {
				char n = openNo.charAt(3);
				if (plan.getPlanNo().contains(n + "")) {
					plan.setStatus(CqsscPlan.CQSSC_PLAN_STATUS_WIN);
					plan.setTimesPrizeIssueNo(planIssueNo);
					plan.setPrizeNo(openNo);
				} else {
					plan.setStatus(CqsscPlan.CQSSC_PLAN_STATUS_NOT_WIN);
					plan.setTimesNo(plan.getTimesNo() + 1);
				}
			}
			if ("个位".equals(plan.getWei())) {
				char n = openNo.charAt(4);
				if (plan.getPlanNo().contains(n + "")) {
					plan.setStatus(CqsscPlan.CQSSC_PLAN_STATUS_WIN);
					plan.setTimesPrizeIssueNo(planIssueNo);
					plan.setPrizeNo(openNo);
				} else {
					plan.setStatus(CqsscPlan.CQSSC_PLAN_STATUS_NOT_WIN);
					plan.setTimesNo(plan.getTimesNo() + 1);
				}
			}
			cqsscPlanRepository.update(plan);
		}
	}

	@Override
	public List<CqsscPlan> findByPlanIssueNo(String planIssueNo) {
		return cqsscPlanRepository.findByPlanIssueNo(planIssueNo);
	}

	@Override
	public List<CqsscPlan> findByPlanIssueDate(String planIssueDate) {
		return cqsscPlanRepository.findByPlanIssueDate(planIssueDate);
	}

	@Override
	public CqsscPlan executeSave(CqsscPlan cqsscPlan) {
		cqsscPlanRepository.save(cqsscPlan);
		return cqsscPlan;
	}

	@Override
	public List<CqsscPlan> findPlanCqsscPlansByPlanIssueNo(String planIssueNo) {
		return cqsscPlanRepository.findPlanCqsscPlansByPlanIssueNo(planIssueNo);
	}

}
